import { createApp } from 'vue'
import { MessageType } from '@/types'
import Message from '../Message.vue'

const createMessage = (message: string, type: MessageType, timeout = 2000) => {
  const messageInstance = createApp(Message, { message, type })
  const mountNode = document.createElement('div')
  document.body.appendChild(mountNode)
  messageInstance.mount(mountNode)
  setTimeout(() => {
    // https://v3.cn.vuejs.org/api/application-api.html#unmount
    messageInstance.unmount()
    document.body.removeChild(mountNode)
  }, timeout)
}

export default createMessage